/*************************************************
Author: tony
Date: 07/23/2013
Description:
Notes:
*************************************************/

#ifndef __CQQMallConfig_h__
#define __CQQMallConfig_h__

#include "commerrno.h"
#include "commclass.h"
#include "CTArray.h"
#include "CTSizeString.h"
#include "CTHashObjectMgr.h"

class CMallGoodsUnit
{
public:
    CMallGoodsUnit ()
    {
        _uiGoodsId = 0;
        _ucItemType = 0;
        _usItemCfgId = 0;
        _iItemCount = 0;
        _iInitialCost = 0;
        _iRealCost = 0;
        _chAttr = 0;
        _bIsNewGoods = false;
        _szDesc.Set("");

        _iDailyTaskTriggerType = 0;
        _iDailyTaskTriggerCount = 0;
    }

    unsigned int _uiGoodsId;
    unsigned char  _ucItemType;
    unsigned short _usItemCfgId;
    int  _iItemCount;
    int  _iInitialCost;  //原价直接读取item表中的金币价格
    int  _iRealCost;
    CTSizeString<MAX_GOODS_DESC_STRING_SIZE> _szDesc;
    char _chAttr;
    bool _bIsNewGoods;

    int _iDailyTaskTriggerType;     //触发小秘书的类型
    int _iDailyTaskTriggerCount;    //触发小秘书的数量
};

class CQQLimitGoodsUnit
{
public:
    CQQLimitGoodsUnit ()
    {
        _uiGoodsId = 0;
        _iMaxBuyCnt = 0;
        _iValidTime = 0;
        _iInValidTime = 0;
        _bLimitByDay = false;
        _bFirstDaySendTips = false;
    }

    unsigned int _uiGoodsId;
    short _iMaxBuyCnt;
    int  _iValidTime;
    int  _iInValidTime;
    bool _bLimitByDay;
    bool _bFirstDaySendTips;
};

class CQuickBuyGoodsUnit
{
public:
    CQuickBuyGoodsUnit ()
    {
        _ucItemType = 0;
        _usItemCfgId = 0;
    }

    unsigned char _ucItemType;
    unsigned short _usItemCfgId;

    bool operator==(const CQuickBuyGoodsUnit& stKey) const
    {
        return (_ucItemType == stKey._ucItemType && _usItemCfgId == stKey._usItemCfgId);
    }
};

enum EN_MALL_GOODS_ATTR
{
    MALL_GOODS_ATTR_HOT = 1,
    MALL_GOODS_ATTR_CHEAP = 2,
    MALL_GOODS_ATTR_LIMITED = 3,
    MALL_GOODS_ATTR_DAYLIMIT = 4,  //按天限量
};

enum EN_MALL_TAB_TYPE
{
    MALL_TAB_LIMIT = 1,
    MALL_TAB_HOT = 2,
    MALL_TAB_CRYSTAL = 3,
    MALL_TAB_PROP = 4,
    MALL_TAB_EQUIP = 5,
    MALL_TAB_GROW_FUND = 6, //成长基金
    MALL_TAB_MOUNT = 7, //坐骑
    MALL_TAB_FASHION = 8 //时装
};

class CQQMallMgr;
class CQQMallConfig
{
public:
    friend class CQQMallMgr;

    CQQMallConfig ()
    {
    }

    int Init (const char * szBuff)
    {
        int iRet = _stMallGoods.Init(szBuff, MAX_GOODS_CONFIG_NUM);
        CHECK_RETURN(iRet);

        int iOffSet = CTLib::CTHashObjectMgr<unsigned int, CMallGoodsUnit>::CountSize(MAX_GOODS_CONFIG_NUM);
        iRet = _stQuickBuyGoods.Init(szBuff + iOffSet, MAX_QUICK_BUY_GOODS_NUM);
        CHECK_RETURN(iRet);

        return iRet;
    }

    static size_t CountSize()
    {
        return  CTLib::CTHashObjectMgr<unsigned int, CMallGoodsUnit>::CountSize(MAX_GOODS_CONFIG_NUM) +
            CTLib::CTHashObjectMgr<CQuickBuyGoodsUnit, unsigned int>::CountSize(MAX_QUICK_BUY_GOODS_NUM);
    }

    int AddGoodUnit (const CMallGoodsUnit & stGoodsUnit)
    {
        int iIndex = _stMallGoods.HashFind(stGoodsUnit._uiGoodsId);
        if (iIndex >= 0)
        {
            return ERROR_QQMALL_GOODS_ID;
        }

        iIndex = _stMallGoods.HashAlloc(stGoodsUnit._uiGoodsId);
        if (iIndex < 0 )
        {
            return ERROR_QQMALL_GOODS_FULL;
        }

        if (stGoodsUnit._iInitialCost < stGoodsUnit._iRealCost) //原价小于打折价格,配置有问题
        {
            return ERROR_QQMALL_GOODS_FMT;
        }

        _stMallGoods[iIndex] = stGoodsUnit;

        return 0;
    }

    bool IsValidGoods (unsigned int uiGoodsId)
    {
        return (_stMallGoods.HashFind(uiGoodsId) >= 0);
    }

    const CMallGoodsUnit & GetGoodUnit (unsigned int uiGoodsId)
    {
        int iIndex = _stMallGoods.HashFind(uiGoodsId);
        if (iIndex < 0)
        {
            static CMallGoodsUnit stUnit;
            return stUnit;
        }

        return _stMallGoods[iIndex];
    }

    int AddLimitGoodCfg (CQQLimitGoodsUnit & stGoodsUnit, int iNowTime)
    {
        if (_stLimitCfg.Size() >= MAX_TAB_GOODS_CFG_LIMITED)
        {
            return ERROR_QQMALL_GOODS_FULL;
        }

        if (!IsValidGoods(stGoodsUnit._uiGoodsId))
        {
            return ERROR_QQMALL_GOODS_NONE;
        }

        if (stGoodsUnit._iInValidTime < 0 || stGoodsUnit._iValidTime < 0
            || stGoodsUnit._iInValidTime <= stGoodsUnit._iValidTime)
        {
            return ERROR_QQMALL_GOODS_FMT;
        }

        if (stGoodsUnit._iInValidTime < iNowTime)
        {
            return 0;
        }

        const CMallGoodsUnit & stCfg = GetGoodUnit(stGoodsUnit._uiGoodsId);
        if (MALL_GOODS_ATTR_DAYLIMIT == stCfg._chAttr)
        {
            stGoodsUnit._bLimitByDay = true;
        }

        _stLimitCfg.AddOneItem(stGoodsUnit);
        return 0;
    }

    int AddGoodCfg (EN_MALL_TAB_TYPE enType, unsigned int uiGoodsId)
    {
        if (!IsValidGoods(uiGoodsId))
        {
            return ERROR_QQMALL_GOODS_NONE;
        }

        switch (enType)
        {
        case MALL_TAB_HOT:
            {
                if (_stHotCfg.Size() >= MAX_TAB_GOODS_CFG_HOT)
                {
                    return ERROR_QQMALL_GOODS_FULL;
                }

                _stHotCfg.AddOneItem(uiGoodsId);
            }
            break;

        case MALL_TAB_CRYSTAL:
            {
                if (_stCrystalCfg.Size() >= MAX_TAB_GOODS_CFG_CRYSTAL)
                {
                    return ERROR_QQMALL_GOODS_FULL;
                }

                _stCrystalCfg.AddOneItem(uiGoodsId);
            }
            break;

        case MALL_TAB_PROP:
            {
                if (_stPropCfg.Size() >= MAX_TAB_GOODS_CFG_PROP)
                {
                    return ERROR_QQMALL_GOODS_FULL;
                }

                _stPropCfg.AddOneItem(uiGoodsId);
            }
            break;

        case MALL_TAB_MOUNT:
            {
                if (_stMountCfg.Size() >= MAX_TAB_GOODS_CFG_MOUNT)
                {
                    return ERROR_QQMALL_GOODS_FULL;
                }
                _stMountCfg.AddOneItem(uiGoodsId);
            }
            break;
        case MALL_TAB_FASHION:
            {
                if (_stFashionCfg.Size() >= MAX_TAB_GOODS_CFG_FASHION)
                {
                    return ERROR_QQMALL_GOODS_FULL;
                }
                _stFashionCfg.AddOneItem(uiGoodsId);
            }
            break;
        default:
            return ERROR_QQMALL_GOODS_FMT;
        }

        return 0;
    }

    int AddQuickBuyGoodCfg (CQuickBuyGoodsUnit & stGoodsUnit, unsigned int uiGoodsId)
    {
        int iIndex = _stQuickBuyGoods.HashFind(stGoodsUnit);
        if (iIndex >= 0)
        {
            return ERROR_QQMALL_GOODS_ID;
        }

        iIndex = _stQuickBuyGoods.HashAlloc(stGoodsUnit);
        if (iIndex < 0 )
        {
            return ERROR_QQMALL_GOODS_FULL;
        }

        if (!IsValidGoods(uiGoodsId))
        {
            return ERROR_QQMALL_GOODS_NONE;
        }

        _stQuickBuyGoods[iIndex] = uiGoodsId;

        return RET_OK;
    }

    bool IsLimitGoodsValid ()
    {
        for (int i = 0; i < _stLimitCfg.Size(); i++)
        {
            unsigned int uiGoodsId = _stLimitCfg[i]._uiGoodsId;

            //显示商品,不能出现在其它的配置表中
            int iIndex = _stQuickBuyGoods.GetHeadIndex();
            while (iIndex >= 0)
            {
                if (uiGoodsId == _stQuickBuyGoods[iIndex])
                {
                    CT_ERROR(("%d", uiGoodsId));
                    return false;
                }

                iIndex = _stQuickBuyGoods.GetNextIndex(iIndex);
            }

            for (int k = 0; k < _stHotCfg.Size(); k++)
            {
                if (uiGoodsId == _stHotCfg[k])
                {
                    CT_ERROR(("%d", uiGoodsId));
                    return false;
                }
            }

            for (int k = 0; k < _stCrystalCfg.Size(); k++)
            {
                if (uiGoodsId == _stCrystalCfg[k])
                {
                    CT_ERROR(("%d", uiGoodsId));
                    return false;
                }
            }

            for (int k = 0; k < _stPropCfg.Size(); k++)
            {
                if (uiGoodsId == _stPropCfg[k])
                {
                    CT_ERROR(("%d", uiGoodsId));
                    return false;
                }
            }

            const CQQLimitGoodsUnit & stSrc = _stLimitCfg[i];
            for (int k = i + 1; k < _stLimitCfg.Size(); k++)
            {
                const CQQLimitGoodsUnit & stTmp = _stLimitCfg[k];
                if (stSrc._uiGoodsId == stTmp._uiGoodsId)
                {
                    //判断时间段是否会有交叉
                    if ((stSrc._iValidTime >= stTmp._iValidTime && stSrc._iValidTime <= stTmp._iInValidTime)
                        || (stSrc._iInValidTime >= stTmp._iValidTime && stSrc._iInValidTime <= stTmp._iInValidTime))
                    {
                        CT_ERROR(("%d, %d", k,stSrc._uiGoodsId));
                        return false;
                    }
                }
            }
        }

        return true;
    }

    bool IsLimitGoods (unsigned int uiGoods)
    {
        for (int i = 0; i < _stLimitCfg.Size(); i++)
        {
            if (uiGoods == _stLimitCfg[i]._uiGoodsId)
            {
                return true;
            }
        }

        return false;
    }

    int GetLimitGoodsConfig (unsigned int uiGoodsId, int iNowTime, CQQLimitGoodsUnit & stConfig)
    {
        for (int i = 0; i < _stLimitCfg.Size(); i++)
        {
            CQQLimitGoodsUnit & stGoods = _stLimitCfg[i];
            if (stGoods._uiGoodsId == uiGoodsId
                && (iNowTime < stGoods._iInValidTime && iNowTime > stGoods._iValidTime))
            {
                stConfig = _stLimitCfg[i];
                return 0;
            }
        }

        return ERROR_QQMALL_GOODS_NONE;
    }

    bool IsMountGoods (unsigned int uiGoodId)
    {
        for (int i = 0; i < _stMountCfg.Size(); i++)
        {
            if (_stMountCfg[i] == uiGoodId)
            {
                return true;
            }
        }

        return false;
    }

private:
    CTHashObjectMgr<unsigned int, CMallGoodsUnit> _stMallGoods;
    CTHashObjectMgr<CQuickBuyGoodsUnit, unsigned int> _stQuickBuyGoods;

    CTArray<CQQLimitGoodsUnit, MAX_TAB_GOODS_CFG_LIMITED> _stLimitCfg;
    CTArray<unsigned int,      MAX_TAB_GOODS_CFG_HOT>     _stHotCfg;
    CTArray<unsigned int,      MAX_TAB_GOODS_CFG_CRYSTAL> _stCrystalCfg;
    CTArray<unsigned int,      MAX_TAB_GOODS_CFG_PROP>    _stPropCfg;
    CTArray<unsigned int,      MAX_TAB_GOODS_CFG_MOUNT>      _stMountCfg;
    CTArray<unsigned int,      MAX_TAB_GOODS_CFG_FASHION>    _stFashionCfg;
};

namespace __gnu_cxx  //
{
    template<> struct hash<CQuickBuyGoodsUnit>
    {
        size_t operator()(const CQuickBuyGoodsUnit& x) const
        {
            return x._ucItemType * 100000 + x._usItemCfgId;
        }
    };
}

#endif // __CQQMallConfig_h__
